//---------------------------------------------------------------------------
//
// Name:        ComprimirFrame.cpp
// Author:      Fernandez Vera Ezequiel, Manzanel Juan Manuel
// Created:     21/02/2009 11:18:52 a.m.
// Description: ComprimirFrame class implementation
//
//---------------------------------------------------------------------------

#include "ComprimirFrame.h"


//Do not add custom headers between
//Header Include Start and Header Include End
//wxDev-C++ designer will remove them
////Header Include Start
////Header Include End

//----------------------------------------------------------------------------
// ComprimirFrame
//----------------------------------------------------------------------------
//Add Custom Events only in the appropriate block.
//Code added in other places will be removed by wxDev-C++
////Event Table Start
BEGIN_EVENT_TABLE(ComprimirFrame,wxFrame)
	////Manual Code Start
	////Manual Code End
	
	EVT_CLOSE(ComprimirFrame::OnClose)
	EVT_BUTTON(ID_BOTONCOMPRIMIR,ComprimirFrame::botonComprimirClick)
END_EVENT_TABLE()
////Event Table End

ComprimirFrame::ComprimirFrame(wxWindow *parent, wxWindowID id, const wxString &title, const wxPoint &position, const wxSize& size, long style)
: wxFrame(parent, id, title, position, size, style)
{
	CreateGUIControls();
    if(dialogoAbrir->ShowModal() == wxID_OK)
        imagen = dialogoAbrir->GetPath();
    compresor = new Compresor();
    this->SetBackgroundColour(wxColour(_("white")));
}

ComprimirFrame::~ComprimirFrame()
{}

void ComprimirFrame::CreateGUIControls()
{
	//Do not add custom code between
	//GUI Items Creation Start and GUI Items Creation End
	//wxDev-C++ designer will remove them.
	//Add the custom code before or after the blocks
	////GUI Items Creation Start

	SetTitle(wxT("ComprimirFrame"));
	SetIcon(wxNullIcon);
	SetSize(9,8,450,271);
	Center();
	

	botonHojasComp = new wxToggleButton(this, ID_BOTONHOJASCOMP, wxT("QuadTreeHojasComp"), wxPoint(295,50), wxSize(120,37), 0, wxDefaultValidator, wxT("botonHojasComp"));
	botonHojasComp->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma")));

	dialogoAbrir =  new wxFileDialog(this, wxT("Eliga una imagen"), wxT(""), wxT(""), wxT("*.bmp"), wxOPEN);

	dialogoGuardar =  new wxFileDialog(this, wxT("Elija donde guardar la imagen comprimida"), wxT(""), wxT(""), wxT("*.*"), wxSAVE);

	botonHojas = new wxToggleButton(this, ID_BOTONHOJAS, wxT("QuadTreeHojas"), wxPoint(165,50), wxSize(120,37), 0, wxDefaultValidator, wxT("botonHojas"));
	botonHojas->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma")));

	botonVector = new wxToggleButton(this, ID_BOTONVECTOR, wxT("QuadTreeVector"), wxPoint(35,50), wxSize(120,37), 0, wxDefaultValidator, wxT("botonVector"));
	botonVector->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma")));

	botonComprimir = new wxButton(this, ID_BOTONCOMPRIMIR, wxT("Comprimir!"), wxPoint(185,186), wxSize(78,37), 0, wxDefaultValidator, wxT("botonComprimir"));
	botonComprimir->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma")));

	editorUmbral = new wxTextCtrl(this, ID_EDITORUMBRAL, wxT(""), wxPoint(186,127), wxSize(75,25), 0, wxDefaultValidator, wxT("editorUmbral"));
	editorUmbral->SetFont(wxFont(8, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Tahoma")));

	WxStaticText2 = new wxStaticText(this, ID_WXSTATICTEXT2, wxT("Ingrese el umbral"), wxPoint(142,92), wxDefaultSize, 0, wxT("WxStaticText2"));
	WxStaticText2->SetFont(wxFont(16, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Trebuchet MS")));

	WxStaticText1 = new wxStaticText(this, ID_WXSTATICTEXT1, wxT("Elija el tipo de quadtree"), wxPoint(110,9), wxDefaultSize, 0, wxT("WxStaticText1"));
	WxStaticText1->SetFont(wxFont(16, wxSWISS, wxNORMAL,wxNORMAL, false, wxT("Trebuchet MS")));
	////GUI Items Creation End
    
    	
}

void ComprimirFrame::OnClose(wxCloseEvent& event)
{
	Destroy();
}

wxString ComprimirFrame::getTipoQuadTree() {
    if((botonVector->GetValue()) && (!botonHojas->GetValue()))
        return _("vector");
    else
        if((!botonVector->GetValue()) && (botonHojas->GetValue()))
            return _("hojas");
}

bool ComprimirFrame::datosCompletos() {
    if((botonVector->GetValue()) && (!botonHojas->GetValue()) && (!botonHojasComp->GetValue()) 
    || (!botonVector->GetValue()) && (botonHojas->GetValue()) && (!botonHojasComp->GetValue())
    || (!botonVector->GetValue()) && (!botonHojas->GetValue()) && (botonHojasComp->GetValue()))
        if ((!editorUmbral->GetValue().IsNull()) && (editorUmbral->GetValue().IsNumber()))
            return true;
    return false;
}

int ComprimirFrame::getUmbral() {
    long umbral;
    wxString aux = editorUmbral->GetValue();
    aux.ToLong(&umbral);
    return umbral; 
}

/*
 * botonComprimirClick
 */
void ComprimirFrame::botonComprimirClick(wxCommandEvent& event) {
    if (datosCompletos()){
        if(dialogoGuardar->ShowModal() == wxID_OK){
            comprimido = dialogoGuardar->GetPath();
            wxString tipoquad(getTipoQuadTree());
            int umbral = getUmbral();
            if (botonVector->GetValue())
                compresor->Comprimir(0,imagen.ToAscii(),comprimido.ToAscii(),umbral);
            else
                if (botonHojas->GetValue())
                    compresor->Comprimir(1,imagen.c_str(),comprimido.c_str(),umbral);
                else
                    if (botonHojasComp->GetValue())
                        compresor->Comprimir(2,imagen.c_str(),comprimido.c_str(),umbral);
            }
        Destroy();
    }
}
